package com.sms.controller;

import com.commons.enums.ResponseCodeEnum;
import com.commons.utils.ResponseResult;
import com.sms.dto.EmailInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * @author mis_wu
 * @date 2022/1/4 14:10
 * 发送邮件、定时发送邮件
 */
@RestController
@RequestMapping("/mail-services")
@Api(tags = "Email-Service-Api")
@Slf4j
public class SendMailController {
    @Autowired
    JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String sender;

    @ApiOperation("简单邮件的发送")
    @PostMapping("/loginSendCode")
    public ResponseResult sendLoginEmailCode(String receiveEmail, HttpSession session){
        log.info("receiveEmail:{}",receiveEmail);
        String token=session.getId();
        int ran = (int) (Math.random()*(25-0)+0);
        String code=token.substring(ran,ran+6);
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送人 必须设置
            mailMessage.setFrom(sender);
            //接收人
            mailMessage.setTo(receiveEmail);
            //邮件主题
            mailMessage.setSubject("登录验证码");
            //邮件内容
            mailMessage.setText("您本次登录的验证码为:"+code+",切记保密,否则后果自负！！！");
            //发送
            mailSender.send(mailMessage);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(ResponseCodeEnum.FAIL.getCode(),"发送失败",e.getMessage());
        }
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(),token);
    }

    @ApiOperation("简单邮件的发送")
    @PostMapping("/simplySend")
    public ResponseResult sendEmail(EmailInfoDto emailInfoDto){
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送人 必须设置
            mailMessage.setFrom(sender);
            //接收人
            mailMessage.setTo(emailInfoDto.getReceiveEmail());
            //邮件主题
            mailMessage.setSubject(emailInfoDto.getEmailTitle());
            //邮件内容
            mailMessage.setText(emailInfoDto.getEmailContext());
            //发送
            mailSender.send(mailMessage);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(ResponseCodeEnum.FAIL.getCode(),"发送失败",e.getMessage());
        }
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode());
    }

    @ApiOperation("带有附件邮件发送")
    @PostMapping("/complicatedSend")
    public ResponseResult sendComplicatedEmail(EmailInfoDto emailInfoDto){
        try {
            //复杂的邮件
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //主题
            helper.setSubject(emailInfoDto.getEmailTitle());
            //正文
            helper.setText("<p>"+emailInfoDto.getEmailContext()+"</p>",true);
            //附件 参数1:收件人显示附件名   参数2:发件人附件路径
            helper.addAttachment(emailInfoDto.getAttachmentName(),new File(emailInfoDto.getAttachmentPath()));
            //收件人
            mimeMessage.setRecipients(Message.RecipientType.TO, emailInfoDto.getReceiveEmail());
            //发件人
            mimeMessage.setFrom(sender);
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(ResponseCodeEnum.FAIL.getCode(),"发送失败",e.getMessage());
        }
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode());
    }

    //1天触发一次
    @Scheduled(cron = "59/1 0/1 0 1/1 * ?")
    public void cron() {
        //1、数据库获取到List<EmailInfoDto>
        //2、for循环调用上面其中一个发送
//        for (List list: emailList){
//            sendEmail(list);
//        }
        log.info("Scheduled success-1天触发一次");
    }
}
